<template>
  <div class="app-container">
    <div class="message">
      <div>
        <h1><span>{{form.outboundTheme}}</span><span>{{form.outboundNo}}</span></h1>
      </div>
      <div>
        <el-form :inline="true" ref="form" type="text" :model="form" :rules="rules" size="medium" label-width="150px">
          <el-form-item label="创建人:">
            <span>{{ form.createBy }}</span>

          </el-form-item>

          <el-form-item label="创建时间:">
            <span>{{ form.createTime }}</span>

          </el-form-item>
          <el-form-item label="最后修改人:">
            <span>{{ form.updateBy }}</span>

          </el-form-item>
          <el-form-item label="修改时间:">
            <span>{{ form.updateTime }}</span>
          </el-form-item>
          <br>
          <el-form-item>
            <el-button plain size="medium" @click="handleDelete" v-hasPermi="['material:outboundOrder:remove']">删除
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <h3>基础信息</h3>
    <el-form :model="queryParams" :rules="rules" ref="queryForm" size="small" :inline="true" v-show="showSearch"
      label-width="68px">
      <el-form-item label="出库类型" prop="outboundType">
        <el-select v-model="queryParams.outboundType" placeholder="请选择出库类型" clearable size="mini">
          <el-option v-for="dict in binding_status" :key="dict.dictValue" :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="关联工单" prop="">
        <el-input
          v-model="queryParams"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="关联项目" prop="projectNo">
        <el-select v-model="queryParams.projectNo" placeholder="请选择关联项目" clearable size="mini">
          <el-option v-for="dict in binding_status" :key="dict.dictValue" :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="出库主题" prop="outboundTheme">
        <el-input v-model="queryParams.outboundTheme" placeholder="请输入出库主题" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="出库时间" prop="outboundTime">
        <el-date-picker v-model="daterangeOutboundTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="出库单号" prop="outboundNo">
        <el-input v-model="queryParams.outboundNo" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="领料人员" prop="pickingPerson">
        <el-input v-model="queryParams.pickingPerson" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="出库人员" prop="outboundPerson">
        <el-input v-model="queryParams.outboundPerson" placeholder="默认当前输入的人员，可自定义输入" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="仓库">
        <el-select v-model="queryParams.warehouseName" placeholder="仓库名称" clearable size="mini">
          <el-option v-for="dict in binding_status" :key="dict.dictValue" :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
        <el-select v-model="queryParams.warehouseArea" placeholder="仓库区域" clearable size="mini">
          <el-option v-for="dict in binding_status" :key="dict.dictValue" :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
    </el-form>

    <h3 style="font-size: 24px;width: 141px;
  height: 55px;
  line-height: 55px;
  text-align: center;
  margin: 10px  0  0  5px;">物料明细</h3>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd1" v-hasPermi="['material:outboundOrder:add']">添加
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain size="mini"  @click="handleDelete"
                   v-hasPermi="['material:outboundOrder:remove']">移除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="tables" style="margin-top:20px;">
      <el-form>
        <el-table border ref="multipleTable" :data="Lit" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChangeMateriallist">
          <el-dialog :title="选择物料" :visible.sync="selectOrder" width="760px" append-to-body>
            <div class="tables">

              <!-- 搜索框 -->
              <el-form style="width: 260px; margin-bottom: 30px; float: right">
                <el-input placeholder="请输入工单名称" v-model="queryParams.procedureName" class="input-with-select"
                          @keyup.enter.native="handleQuery">
                  <el-button slot="append" icon="el-icon-search" @click="handleQuery"></el-button>
                </el-input>
              </el-form>
              <el-form ref="form" :model="form" label-width="80px">
                <!-- 已选 几项 -->
                <!-- <div style="font-size: 20px">
                  已勾选<span>{{ valLength }}</span>项
                </div> -->
                <el-table v-loading="loading" :data="WarehouseInfoList" @selection-change="handleSelectionChange1">

                  <el-table-column type="selection" width="55" align="center"/>
                  <el-table-column label="序号" width="55" align="center" prop="id"/>
                  <el-table-column label="物料名称" width="100" align="center" prop="materialName"/>
                  <el-table-column label="物料类别" width="100" align="center" prop="materialType"/>
                  <el-table-column label="规格" width="100" align="center" prop="specifications"/>
                  <el-table-column label="库存数量" width="100" align="center" prop="remainderCount"/>
                  <el-table-column label="数量" width="200px" align="center" prop="nums">
                    <template slot-scope="scope" >
                        <el-input-number v-model="scope.row.count" @change="inputChangeNumber(scope.row)" controls-position="right"
                                         :min="0" :max="scope.row.remainderCount" label="描述文字">
                        </el-input-number>
                    </template>
                  </el-table-column>

                </el-table>
              </el-form>
              <div class="block">

                <pagination
                  v-show="total > 0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="tianjia"
                />
              </div>


              <div slot="footer" class="dialog-footer" style="margin-top:20px;">
                <el-button @click="Off">取 消</el-button>

                <el-button
                  plain
                  @click="adds"
                >保存
                </el-button>
              </div>
            </div>
          </el-dialog  >


          <!-- @selection-change="handleSelectionChange" -->
          <el-table-column label="序号" prop="id" width="120">
          </el-table-column>
          <el-table-column prop="materialType" label="物料类别" width="120">
          </el-table-column>
          <el-table-column prop="materialName" label="物品名称" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="receiptMaterialNo" label="出库单号" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="specifications" label="规格" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="unit" label="单位" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="remainderCount" label="库存数量" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="本次出库数量" align="center"  prop="nums">
            <template slot-scope="scope" >
              <el-input-number v-model="scope.row.count" @change="inputChangeNumber(scope.row)" controls-position="right"
                               :min="0" :max="scope.row.remainderCount" label="描述文字">
              </el-input-number>
            </template>
          </el-table-column>
        </el-table>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="Off">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {
  listOutboundOrder,
  getOutboundOrder,
  delOutboundOrder,
  addOutboundOrder,
  Querying,
  updateOutboundOrder, WarehouseInfo
} from "@/api/material/outboundOrder";
import {listManagement} from "../../../api/base/management";

export default {
  name: "OutboundOrder",
  dicts: ["binding_status", "partnership_level"],
  data() {
    return {
      nums: "10",
      selectOrder: false,
      // 遮罩层
      loading: true,
      Lit2: [],
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMaterialInfo: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库表格数据
      outboundOrderList: [],
      // 物料信息表格数据
      materialInfoList: [],
      // 库存表格数据
      WarehouseInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeOutboundTime: [],
      // 查询参数
      queryParams: {
        id: '',
        pageNum: 1,
        pageSize: 10,
        outboundNo: null,
        orderNo: null,
        projectNo: null,
        warehouseName: null,
        warehouseArea: null,
        outboundPerson: null,
        outboundTime: null,
        pickingPerson: null,
        outboundTheme: null,
        materialName: null,
        materialType: null,
        specifications: null,
        remainderCount: null,


      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        outboundTheme: [
          {required: true, message: "出库主题不能为空", trigger: "change"},
        ],
      }
    };
  },
  created() {
    Querying(this.$route.query.id).then((response) => {
      this.form = response.data;
      for(let i=0;i<response.data.length;i++){
        response.data[i].count=0;
      }
      this.Lit = JSON.parse(JSON.stringify(response.data.materialInfoList));
      this.Lit2 = JSON.parse(JSON.stringify(response.data.materialInfoList));

    });
    // this.getList();
  },
  methods: {

    inputChangeNumber(value) {

    },
    adds() {
      let newArr=[...this.Lit2,...this.ids].map(function(value, index, array) {return array[index].id})
      let nonUnique = [...new Set(newArr)];
      if (nonUnique.length<newArr.length){
        this.$message('请勿重复勾选');
      }else{
        this.Lit.length=[];
        this.Lit.push(...this.Lit2, ...this.ids);
      }

    },
    /** 查询出库列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOutboundTime && '' != this.daterangeOutboundTime) {
        this.queryParams.params["beginOutboundTime"] = this.daterangeOutboundTime[0];
        this.queryParams.params["endOutboundTime"] = this.daterangeOutboundTime[1];
      }
      listOutboundOrder(this.queryParams).then(response => {
        this.outboundOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outboundNo: null,
        outboundType: null,
        orderNo: null,
        projectNo: null,
        warehouseName: null,
        warehouseArea: null,
        outboundTheme: null,
        outboundPerson: null,
        outboundTime: null,
        pickingPerson: null,
        createTime: null,
        createBy: null,
        status: "0",
        comment: null,
        materialName: null,
        materialType: null,
        remainderCount: null,
        specifications: null,
      };
      this.materialInfoList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOutboundTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange1(selection) {
      this.ids = selection;
      console.log(selection);
    },
    /** 物料明细表选择框 */
    handleSelectionChangeMateriallist(selection) {
      this.MaterialList = selection;
    },
    /** 新增按钮操作 */
    handleAdd1() {
      this.selectOrder = true;

      WarehouseInfo(this.queryParams).then(response => {
        this.WarehouseInfoList = response.dataTable.rows;
        this.total = response.dataTable.total;
        this.loading = false;
      });
    },
    Off() {
      this.selectOrder = false;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.materialInfoList = this.Lit;
          console.log(this.form);
          if (this.form.id != null) {
            updateOutboundOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOutboundOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let newArr=this.MaterialList.map(function(value, index, array) {return array[index].id})
      console.log(this.Lit2.length)
      this.Lit2=this.Lit2.filter(e=> newArr.indexOf(e.id)==-1);
      this.ids=this.ids.filter(e=>newArr.indexOf(e.id)==-1);
      this.Lit.length=[];
      this.Lit.push(...this.Lit2, ...this.ids);

    },
    /** 物料信息序号 */
    rowMaterialInfoIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 物料信息添加按钮操作 */
    handleAddMaterialInfo() {
      let obj = {};
      obj.projectNo = "";
      obj.materialNo = "";
      obj.materialName = "";
      obj.materialType = "";
      obj.specifications = "";
      obj.unit = "";
      obj.count = "";
      obj.comment = "";
      this.materialInfoList.push(obj);
    },
    /** 物料信息删除按钮操作 */
    handleDeleteMaterialInfo() {
      if (this.checkedMaterialInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的物料信息数据");
      } else {
        const materialInfoList = this.materialInfoList;
        const checkedMaterialInfo = this.checkedMaterialInfo;
        this.materialInfoList = materialInfoList.filter(function (item) {
          return checkedMaterialInfo.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMaterialInfoSelectionChange(selection) {
      this.checkedMaterialInfo = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('material/outboundOrder/export', {
        ...this.queryParams
      }, `outboundOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
h2 {
  width: 141px;
  height: 55px;
  line-height: 55px;
  text-align: center;
  border-bottom: 1px solid red;
  margin: 60px 0 0 5px;

}

.el-input__inner {
  border: 1px solid;
}

.dialog-footer {
  text-align: center;
  margin-top: 15px;

}

.outers .headers {
  width: 100%;
  height: 50px;

  border-bottom: 1px solid gainsboro;

  h1 {
    margin-left: 50px;
    padding-top: 10px;
  }
}

.bottom {
  margin-left: 30px;
  margin-top: 0px;

  .adds {
    display: flex;
    margin-bottom: 10px;
  }
}

.el-select .el-input {
  width: 130px;
}

.input-with-select .el-input-group__prepend {
  background-color: #fff;
}

.message {
  padding-left: 20px;
  width: 50% vh;
  height: 170px;
  margin-right: 20px;
  margin-left: 20px;
  margin-bottom: 20px;
  border: 1px solid gainsboro;
  border-radius: 1rem;
  margin-top: 15px;

  span {
    font-size: 20px;
    margin-right: 120px;
  }
}

.gongying {
  width: 100%;
  text-align: center;

  tr {
    height: 50px;
    line-height: 50px;
  }
}
</style>
